package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.ZZZ;
import com.googlecode.jmicroerp.model.Magazin;
import com.googlecode.jmicroerp.repository.GenericProperty;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import java.util.Date;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Ionel.David
 */
public class EditareZZZPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<ZZZ> zzzService;
    @SpringBean(name = "genericService")
    private GenericService<Magazin> magazinService;
    private ZZZ zzz;
    private Magazin magazin;
    
    public EditareZZZPage() {
        zzz = new ZZZ();
        initPage();
    }
    
    public EditareZZZPage(Long idZZZ) {
        zzz = zzzService.getById(ZZZ.class, idZZZ);
        initPage();
    }
    
    
    private void initPage() {
        Form<ZZZ> form = new Form<ZZZ>("form");
        form.setModel(new CompoundPropertyModel<ZZZ>(zzz));
        
/*         final DropDownChoice<Magazin> ddcMagazin = new DropDownChoice<Magazin>("magazin", 
                new PropertyModel<Magazin>(this, "magazin"),
                new LoadableDetachableModel<List<Magazin>>() {

            @Override
            protected List<Magazin> load() {
                return magazinService.getList(Magazin.class, null, null);
            }
        }, new ChoiceRenderer<Magazin>("denumire", "id"));
        form.add(ddcMagazin); */
        
        DropDownChoice<Magazin> ddcMagazin = new DropDownChoice<Magazin>("magazin", new LoadableDetachableModel<List<Magazin>>() {

            @Override
            protected List<Magazin> load() {
                return magazinService.getList(Magazin.class, null, null);
            }
        }, new ChoiceRenderer<Magazin>("denumire", "id"));
        form.add(ddcMagazin);
        
        DateTextField dtfDataAdaugarii = new DateTextField("dataAdaugarii");
        dtfDataAdaugarii.add(new DatePicker());
        form.add(dtfDataAdaugarii);
        
        TextField<BigDecimal> tfCantitate = new TextField<BigDecimal>("valoareTotala");
        tfCantitate.setRequired(true);
        form.add(tfCantitate);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        SubmitLink salvare = new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (zzz.getId() == null) {
                    zzzService.insert(zzz);
                } else {
                    zzzService.update(zzz);
                }
                setResponsePage(VizualizareZZZPage.class);
            }
            
        };
        form.add(salvare);
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(VizualizareZZZPage.class);
            }
            
        });

        add(form);
    }
}
